//
//  S1mpl3HeadInfoViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "S1mpl3HeadInfoViewController.h"
#import "TwoToOne.h"
#import "FitHeader.h"
#import "XHPathCover.h"
#import "BaseView.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GaussianBlur.h"
#import "TabBarController.h"
#import "S1mpl3HeadInfoView.h"
#import "S1mpl3TitleTableViewCell.h"
#import "S1mpl3AlbumTableViewCell.h"
#import "MJRefresh.h"
#import "LZPlayViewController.h"
#import "LZBillboardModel.h"
#import "TabBarController.h"
#import "MBProgressHUD.h"
#import "UIImage+GIF.h"

@interface S1mpl3HeadInfoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableDictionary *dataSourceDic;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,assign)NSInteger s1mpl3Flag;
@property(nonatomic,strong)BaseView *s1mpl3HeadInfoView;



@property(nonatomic,strong)XHPathCover *pathCoverView;
@property(nonatomic,strong)UIImageView *headImageView;
@property(nonatomic,strong)BaseLabel *titleLabel;
@property(nonatomic,strong)UIImageView *vImageView;
@property(nonatomic,strong)BaseLabel *contentLabel;
@property(nonatomic,strong)BaseView *circleView;
@property(nonatomic,strong)BaseView *focusView;
@property(nonatomic,strong)BaseLabel *focusNumLabel;
@property(nonatomic,strong)BaseLabel *fusNumLabel;
@property(nonatomic,strong)BaseLabel *likeNumLabel;
@property(nonatomic,assign)NSInteger albumNum;
@property(nonatomic, strong)NSMutableArray *LZarray;

@property(nonatomic, strong)MBProgressHUD *progressView;

@end

@implementation S1mpl3HeadInfoViewController

-(void)viewWillAppear:(BOOL)animated{
    
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    [[[_progressView subviews]firstObject] removeFromSuperview];
    [[[_progressView subviews] objectAtIndex:1] removeFromSuperview];
    [_progressView setMode:0];
    [_progressView show:YES];
    _progressView.color = [UIColor clearColor];
    _progressView.opaque = YES;
    NSString  *name = @"RED.gif";
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(-70 * FITWidth, -80 * FITHeight, 140 * FITWidth, 100 * FITHeight)];
    imageView.layer.masksToBounds = YES;
    imageView.layer.cornerRadius = 10;
    imageView.opaque = NO;
    [[[[[_progressView subviews] lastObject] subviews] firstObject] removeFromSuperview];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
        NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
        imageView.image = [UIImage sd_animatedGIFWithData:imageData];
    });
    
    
    [[[_progressView subviews] lastObject] addSubview:imageView];
    [self.view addSubview:_progressView];
    
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]];
    //    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName]];
}

-(void)getDataHead{
//    NSLog(@"%@", [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?toUid=%@&device=android", [_s1mpl3HeadDataSourceDic objectForKey:@"uid"]]);
    [TwoToOne getDataByString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?toUid=%@&device=android", [_s1mpl3HeadDataSourceDic objectForKey:@"uid"]] BodyString:nil WithDataBlock:^(id data) {
        _albumNum = [[data objectForKey:@"tracks"] integerValue];
        _s1mpl3HeadInfoView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300 * FITHeight)];
        [self creatView];
        [_pathCoverView setBackgroundImage:[UIImage imageWithBlurImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[data objectForKey:@"backgroundLogo"]]]] intputRadius:0]];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:[data objectForKey:@"mobileSmallLogo"]] placeholderImage:[UIImage imageNamed:@"100*100"]];
        _titleLabel.text = [data objectForKey:@"nickname"];
        _titleLabel.textAlignment = 1;
        _contentLabel.text = [data objectForKey:@"personalSignature"];
        _contentLabel.textAlignment = 1;
        _focusNumLabel.text = [NSString stringWithFormat:@"%@", [data objectForKey:@"followings"]];
        if ([_fusNumLabel.text integerValue] < 10000.0) {
            _fusNumLabel.text = [NSString stringWithFormat:@"%@", [data objectForKey:@"followers"]];
        }else{
            _fusNumLabel.text = [NSString stringWithFormat:@"%.1f万", [[data objectForKey:@"followers"] integerValue] / 10000.0];
        }
        _likeNumLabel.text = [NSString stringWithFormat:@"%@", [data objectForKey:@"favorites"]];
        self.tableView.tableHeaderView = _s1mpl3HeadInfoView;
        [_tableView reloadData];
        [_progressView removeFromSuperview];
    }];
}

-(void)getDataFoot{
//    NSLog(@"%@", [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/1/30?device=android", [_s1mpl3HeadDataSourceDic objectForKey:@"uid"]]);
    [TwoToOne getDataByString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/%ld/30?device=android", [_s1mpl3HeadDataSourceDic objectForKey:@"uid"], _page] BodyString:nil WithDataBlock:^(id data) {
            if ([[data  objectForKey:@"list"]count] == 0) {
                [_tableView.mj_footer endRefreshing];
                _tableView.mj_footer.state = MJRefreshStateNoMoreData;
                return ;
            }
            for (NSDictionary *dic in [data objectForKey:@"list"]) {
                LZBillboardModel *lzBill = [[LZBillboardModel alloc]init];
                [lzBill setValuesForKeysWithDictionary:dic];
                [_LZarray addObject:lzBill];
                [_dataSourceArray addObject:dic];
            }
            [_tableView reloadData];
            //        [_tableView.header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        [_progressView removeFromSuperview];


    }];
}

-(void)tapAction:(UITapGestureRecognizer *)tap{
    CGRect rect = [_contentLabel.text boundingRectWithSize:CGSizeMake(280 * FITWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil] context:nil];
    if (_s1mpl3Flag == 0) {
        [UIView animateWithDuration:.5 animations:^{
            _headImageView.transform = CGAffineTransformRotate(_headImageView.transform, 15);
            _headImageView.center = CGPointMake(_headImageView.center.x, _headImageView.center.y - ceill(rect.size.height));
            _titleLabel.center = CGPointMake(_titleLabel.center.x, _titleLabel.center.y - ceill(rect.size.height));
               _contentLabel.frame = CGRectMake(_contentLabel.frame.origin.x, _contentLabel.frame.origin.y, _contentLabel.frame.size.width, ceill(rect.size.height));
            _contentLabel.center = CGPointMake(_contentLabel.center.x, _contentLabel.center.y - ceill(rect.size.height));
            
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:1 animations:^{
                _headImageView.hidden = YES;
            
            }];
        }];
        _s1mpl3Flag = 1;
    }else{
        [UIView animateWithDuration:.5 animations:^{
            _headImageView.hidden = NO;
            _headImageView.transform = CGAffineTransformRotate(_headImageView.transform, -15);
            _headImageView.center = CGPointMake(_headImageView.center.x, _headImageView.center.y + ceill(rect.size.height));
            _titleLabel.center = CGPointMake(_titleLabel.center.x, _titleLabel.center.y + ceill(rect.size.height));
            _contentLabel.center = CGPointMake(_contentLabel.center.x, _contentLabel.center.y + ceill(rect.size.height));
        } completion:^(BOOL finished) {
            _contentLabel.frame = CGRectMake(_contentLabel.frame.origin.x, _contentLabel.frame.origin.y, _contentLabel.frame.size.width, 20 * FITHeight);
            
        }];
        _s1mpl3Flag = 0;
    }
    
}

-(void)creatView{
    _pathCoverView = [[XHPathCover alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 210 * FITHeight)];
    _pathCoverView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_pathCoverView addGestureRecognizer:tap];
    [_s1mpl3HeadInfoView addSubview:_pathCoverView];
    
    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(142 * FITWidth, 64, 80 * FITHeight, 80 * FITHeight)];
    _headImageView.layer.masksToBounds = YES;
    _headImageView.layer.cornerRadius = 40 * FITHeight;
    [_s1mpl3HeadInfoView addSubview:_headImageView];
    
    _titleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(120 * FITWidth, _headImageView.frame.origin.y + _headImageView.frame.size.height, 120 * FITHeight, 25)];
    [_titleLabel setFontSize:18];
    _titleLabel.textColor = [UIColor whiteColor];
    [_s1mpl3HeadInfoView addSubview:_titleLabel];
    
    _vImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_titleLabel.frame.origin.x + _titleLabel.frame.size.width, _titleLabel.frame.origin.y, 26 * FITHeight, 26 * FITHeight)];
    _vImageView.layer.cornerRadius = 13 * FITHeight;
    _vImageView.layer.masksToBounds = YES;
    [_s1mpl3HeadInfoView addSubview:_vImageView];
    
    _contentLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(60 * FITWidth, _titleLabel.frame.origin.y + _titleLabel.frame.size.height, 280 * FITWidth, 20 * FITHeight)];
    _contentLabel.textColor = [UIColor whiteColor];
    _contentLabel.numberOfLines = 0;
    [_contentLabel setFontSize:16];
    [_s1mpl3HeadInfoView addSubview:_contentLabel];
    
    
    _circleView = [[BaseView alloc] initWithFrame:CGRectMake(65 * FITWidth, 215 * FITHeight, 110 * FITWidth, 35 * FITHeight)];
    UIImageView *focusImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20 * FITWidth, 5 * FITHeight, 20 * FITHeight, 20 * FITHeight)];
    focusImageView.image = [UIImage imageNamed:@"S1mpl3Big圈子"];
    [_circleView addSubview:focusImageView];
    BaseLabel *circleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(focusImageView.frame.origin.x + focusImageView.frame.size.width + 5 * FITWidth, 0, 50 * FITWidth, 35 * FITHeight)];
    circleLabel.textColor = [UIColor lightGrayColor];
    circleLabel.text = @"圈子";
    [_circleView addSubview:circleLabel];
    _circleView.layer.borderWidth = 1;
    _circleView.layer.cornerRadius = 5;
    _circleView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [_s1mpl3HeadInfoView addSubview:_circleView];
    
    _focusView = [[BaseView alloc] initWithFrame:CGRectMake(_circleView.frame.origin.x + _circleView.frame.size.width + 20 * FITWidth, _circleView.frame.origin.y, _circleView.frame.size.width, _circleView.frame.size.height)];
    UIImageView *focusImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(20 * FITWidth, 5 * FITHeight, 20 * FITHeight, 20 * FITHeight)];
    focusImageView1.image = [UIImage imageNamed:@"S1mpl3Big加"];
    [_focusView addSubview:focusImageView1];
    BaseLabel *focusLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(focusImageView.frame.origin.x + focusImageView.frame.size.width + 5 * FITWidth, 0, circleLabel.frame.size.width, circleLabel.frame.size.height)];
    focusLabel.text = @"关注";
    focusLabel.textColor = [UIColor whiteColor];
    _focusView.backgroundColor = [UIColor orangeColor];
    [_focusView addSubview:focusLabel];
    _focusView.layer.cornerRadius = 5;
    [_s1mpl3HeadInfoView addSubview:_focusView];

    _focusNumLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(0, _circleView.frame.origin.y + _circleView.frame.size.height + 5 * FITHeight, self.view.frame.size.width / 3, 20 * FITHeight)];
    _focusNumLabel.textAlignment = 1;
    [_s1mpl3HeadInfoView addSubview:_focusNumLabel];
    
    BaseLabel *focuslittleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(_focusNumLabel.frame.origin.x, _focusNumLabel.frame.origin.y + _focusNumLabel.frame.size.height, _focusNumLabel.frame.size.width, 20 * FITHeight)];
    focuslittleLabel.textAlignment = 1;
    focuslittleLabel.text = @"关注";
    focuslittleLabel.textColor = [UIColor lightGrayColor];
    [_s1mpl3HeadInfoView addSubview:focuslittleLabel];
    
    _fusNumLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(_focusNumLabel.frame.origin.x + _focusNumLabel.frame.size.width, _focusNumLabel.frame.origin.y, self.view.frame.size.width / 3, _focusNumLabel.frame.size.height)];
    _fusNumLabel.textAlignment = 1;
    [_s1mpl3HeadInfoView addSubview:_fusNumLabel];
    
    BaseLabel *fuslittleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(_fusNumLabel.frame.origin.x, _fusNumLabel.frame.origin.y + _fusNumLabel.frame.size.height, _fusNumLabel.frame.size.width, focuslittleLabel.frame.size.height)];
    fuslittleLabel.textAlignment = 1;
    fuslittleLabel.text = @"粉丝";
    fuslittleLabel.textColor = [UIColor lightGrayColor];
    [_s1mpl3HeadInfoView addSubview:fuslittleLabel];
    
    _likeNumLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(_fusNumLabel.frame.origin.x + _fusNumLabel.frame.size.width, _fusNumLabel.frame.origin.y, self.view.frame.size.width / 3, _fusNumLabel.frame.size.height)];
    _likeNumLabel.textAlignment = 1;
    [_s1mpl3HeadInfoView addSubview:_likeNumLabel];
    
    BaseLabel *likelittleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(_likeNumLabel.frame.origin.x, _likeNumLabel.frame.origin.y + _likeNumLabel.frame.size.height, _likeNumLabel.frame.size.width, focuslittleLabel.frame.size.height)];
    likelittleLabel.text = @"赞过的";
    likelittleLabel.textAlignment = 1;
    likelittleLabel.textColor = [UIColor lightGrayColor];
    [_s1mpl3HeadInfoView addSubview:likelittleLabel];
    
}

-(void)loadView{
    [super loadView];
    _page = 1;
    self.dataSourceArray = [NSMutableArray array];
    self.dataSourceDic = [NSMutableDictionary dictionary];
    _LZarray = [NSMutableArray array];
    [self getDataHead];
    [self getDataFoot];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, self.view.frame.size.width, self.view.frame.size.height + 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

    _s1mpl3HeadInfoView = [[S1mpl3HeadInfoView alloc] initWithFrame:CGRectMake(0, 0, 375 * FITWidth, 500 * FITHeight)];
//    _s1mpl3HeadInfoView.backgroundColor = [UIColor cyanColor];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getDataFoot];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count + 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 40 * FITHeight;
    }
    return 130 * FITHeight;
}

#pragma mark - 点击跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_dataSourceArray.count == 0) {
        return;
    }
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[LZPlayViewController shareInstance]];
    [LZPlayViewController shareInstance].dataArray = _LZarray;
    [LZPlayViewController shareInstance].dicArray = _dataSourceArray;
    [LZPlayViewController shareInstance].index = indexPath.row;
    [LZPlayViewController shareInstance].dic = _dataSourceArray[indexPath.row];
    [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"cell0"];
    [self presentViewController:nav animated:YES completion:nil];
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identitier = @"cell";
    if (indexPath.row == 0) {
        S1mpl3TitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identitier];
        if (!cell) {
            cell = [[S1mpl3TitleTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identitier];
        }
        cell.albumNum = [NSString stringWithFormat:@"%ld", _albumNum];
        return cell;
    }
    else{
    static NSString *identifier1 = @"cell1";
    S1mpl3AlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
    if (!cell) {
        cell = [[S1mpl3AlbumTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
        
    }
    cell.dic = [_dataSourceArray objectAtIndex:indexPath.row - 1];
    
    return cell;
    }

}

#pragma mark- scroll delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [_pathCoverView scrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [_pathCoverView scrollViewDidEndDecelerating:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [_pathCoverView scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [_pathCoverView scrollViewWillBeginDragging:scrollView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"S1mpl3透明图"] forBarMetrics:UIBarMetricsDefault];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
